#!/bin/bash
# zabbix server4.0安装

#检查操作系统版本，该脚本只能运行在 Centos 7.x 系统上
cat /etc/redhat-release |grep -i centos |grep '7.[[:digit:]]' &>/dev/null
if [[ $? != 0 ]]
then
    echo -e "\e[1;31m不支持的操作系统，该脚本只适用于CentOS 7.x  x86_64 操作系统\e[0m"
    exit 1
fi

host_ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
zabbix_pwd=DDidc0623*
down_url="https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm"

echo -e "\e[1;31m此脚本为Zabbix_server4.0安装脚本\e[0m"
echo -e "\e[1;34m请确认本机已配置好epel源和阿里源，并未安装mysql数据库和nginx（稍后脚本会自动安装数据库和httpd）！\e[0m"
echo -e "\e[1;31m暂停10秒后开始安装...\n\e[0m"
sleep 10

install_mariadb() {
	echo -e "\e[1;34m安装mariadb10.1中，请稍后..\e[0m"
	[ ! -f "/etc/yum.repos.d/mariadb.repo" ] && touch /etc/yum.repos.d/mariadb.repo
	cat >> /etc/yum.repos.d/mariadb.repo <<-EOF
	[mariadb]
	name=MariaDB
	baseurl=https://mirrors.ustc.edu.cn/mariadb/yum/10.1/centos7-amd64
	gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
	gpgcheck=1
	EOF
	yum install -y mariadb-server mariadb 1> /dev/null
	if [ $? -eq 0 ];then
		systemctl enable mariadb
		systemctl start mariadb
		if [ $? = 0 ];then 
			mysql -e "create database zabbix character set utf8 collate utf8_bin;"
			mysql -e "grant all privileges on zabbix.* to zabbix@'localhost' identified by '${zabbix_pwd}';"
			mysql -e "flush privileges;"
		else
			echo -e "\e[1;31mERROR：Mariadb启动失败\e[0m"
			exit 2
		fi
	else
		echo -e "\e[1;31mERROR：安装Mariadb失败\e[0m"
		exit 2
	fi
}

install_zabbix() {
	echo -e "\e[1;34m正在配置zabbix4.2的yum源\e[0m"
	rpm -Uvh $down_url
	sed -i 's/repo.zabbix.com/mirrors.aliyun.com\/zabbix/g' /etc/yum.repos.d/zabbix.repo
	yum clean all &> /dev/null
	echo -e "\e[1;34m正在安装zabbix，请耐心等待...\e[0m"
	yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent zabbix-get 1> /dev/null
	if [ $? -eq 0 ];then
		echo -e "\e[1;34m正在导入数据,请稍后..\e[0m"
		zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql zabbix
	else
		echo -e "\e[1;31mERROR：下载zabbix失败\e[0m"
		exit 3
	fi
}

zabbix_conf() {
	sed -i "/^# StartPingers=1/a StartPingers=10" /etc/zabbix/zabbix_server.conf
	sed -i "/^# StartPollers=5/a StartPollers=10" /etc/zabbix/zabbix_server.conf
	sed -i "/^# StartTrappers=5/a StartTrappers=20" /etc/zabbix/zabbix_server.conf
	sed -i "/^# CacheSize=8M/a CacheSize=256M" /etc/zabbix/zabbix_server.conf
	sed -i "/^# CacheUpdateFrequency=60/a CacheUpdateFrequency=180" /etc/zabbix/zabbix_server.conf
	sed -i "/^# HistoryCacheSize=16M/a HistoryCacheSize=64M" /etc/zabbix/zabbix_server.conf
	sed -i "/^# HistoryIndexCacheSize=4M/a HistoryIndexCacheSize=10M" /etc/zabbix/zabbix_server.conf
	sed -i "s/Timeout=4/Timeout=6/" /etc/zabbix/zabbix_server.conf
	sed -i "/^# TrendCacheSize=4M/a TrendCacheSize=128M" /etc/zabbix/zabbix_server.conf
	sed -i "/^# ValueCacheSize=8M/a ValueCacheSize=128M" /etc/zabbix/zabbix_server.conf
	sed -i "/^# DBPassword=/a DBPassword=$zabbix_pwd" /etc/zabbix/zabbix_server.conf
	sed -i "s/LogFileSize=0/LogFileSize=5/" /etc/zabbix/zabbix_server.conf
	sed -i 's/# php_value date.timezone Europe\/Riga/php_value date.timezone Asia\/Shanghai/' /etc/httpd/conf.d/zabbix.conf
	cp /usr/share/zabbix/conf/zabbix.conf.php.example /etc/zabbix/web/zabbix.conf.php
	sed -i "10c \$DB[\"PASSWORD\"]    		= '$zabbix_pwd';" /etc/zabbix/web/zabbix.conf.php
}


install_mariadb
install_zabbix
zabbix_conf
systemctl start zabbix-server zabbix-agent httpd
if [ $? -eq 0 ];then
	systemctl enable zabbix-server zabbix-agent httpd
	clear
	echo -e "\e[1;34m已启动zabbix服务端,接下来请在${host_ip}/zabbix页面进行web设置\n登录用户为Admin,密码为$zabbix_pwd\e[0m"
	echo -e "\e[1;34m如果web界面的数据库IP,密码端口等信息设置失误可以在/etc/zabbix/web/zabbix.conf.php文件内修改\n\e[0m"
	echo -e "\e[1;32m数据库密码还未设置，如有需要请手动设置\e[0m"
	echo -e "\e[1;32m如web界面无法连接数据库,请检查zabbix用户是否能登陆数据库,检查用户表将用户名为空值的用户删掉重启zabbix即可\e[0m"
	echo -e "\e[1;32m删除空值用户命令：delete from mysql.user where user='';\e[0m"

else
	echo -e "\e[1;31mERROR：启动zabbix失败\e[0m"
	exit 4
fi